﻿(function ($) {
    $.fn.ServiceProxy = function (url) {
        var Url = url;
        var Data = {};
        var ContentType = "application/json; charset=utf-8";
        var DataType = "json";
        var ProcessData = false;
        this.CallService = function (TYPE, operation, data, success, failure) {
            //var me = this;
            var serviceUrl = Url + "/" + operation;
            $.ajax({
                type: TYPE,
                url: serviceUrl,
                data: data == null ? Data : data,
                contentType: ContentType,
                dataType: DataType,
                processdata: ProcessData,
                success: function (msg) {
                    if (success == null) {
                        ServiceSucceeded(msg);
                    }
                    else {
                        success(msg.d);
                    }
                },
                error: (failure == null) ? ServiceFailed : failure
            });
        }

        var ServiceFailed = function (xhr) {
            // alert(xhr.responseText);
            if (xhr.responseText) {
                var err = xhr.responseText;
                if (err)
                    error(err);
                else
                    error({ Message: "Unknown server error." })
            }
            return;
        }

        var ServiceSucceeded = function (result) {
            if (this.dataType == "jsonp") {

                resultObject = result.GetEmployeeResult;
                var string = result.Name + " \n " + result.Address;
                // alert(string);
            }
        }
        return this;
    };
})(jQuery);